package com.csproj.system.service;


import com.csproj.siiat.base.common.dto.BaseDTO;
import com.csproj.siiat.security.SecurityUtils;
import com.csproj.system.domain.Dictionary;
import com.csproj.system.repository.DictionaryRepository;
import com.csproj.system.service.dto.DictionaryDTO;
import com.csproj.system.service.mapper.DictionaryMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

/**
 * Service Implementation for managing Dictionary.
 */
@Service
@Transactional
public class DictionaryService {

    private final Logger log = LoggerFactory.getLogger(DictionaryService.class);

    private final DictionaryRepository dictionaryRepository;

    private final DictionaryMapper dictionaryMapper;

    public DictionaryService(DictionaryRepository dictionaryRepository, DictionaryMapper dictionaryMapper) {
        this.dictionaryRepository = dictionaryRepository;
        this.dictionaryMapper = dictionaryMapper;
    }

    /**
     * Save a dictionary.
     *
     * @param dictionaryDTO the entity to save
     * @return the persisted entity
     */
    @CacheEvict(value = "dictCache")
    public DictionaryDTO save(DictionaryDTO dictionaryDTO) {
        log.debug("Request to save Dictionary : {}", dictionaryDTO);
        Dictionary dictionary = dictionaryMapper.toEntity(dictionaryDTO);
//        DomainUtils.setHistory(dictionary, Dictionary.class, dictionaryDTO, DictionaryDTO.class);
        dictionary.setDeletedFlag(false);
        dictionary.setCreationTime(ZonedDateTime.now());
        dictionary.setCreatorUserId(SecurityUtils.getCurrentUserId());
        dictionary = dictionaryRepository.save(dictionary);
        return dictionaryMapper.toDto(dictionary);
    }

    /**
     *  Get all the dictionaries.
     *
     *  @return the list of entities
     */
    @Transactional(readOnly = true)
    public List<DictionaryDTO> findAll() {
        log.debug("Request to get all Dictionaries");
        List<DictionaryDTO> result = dictionaryRepository.findAll().stream()
            .map(dictionaryMapper::toDto)
            .collect(Collectors.toCollection(LinkedList::new));

        return result;
    }

    /**
     *  Get one dictionary by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Transactional(readOnly = true)
    public DictionaryDTO findOne(String id) {
        log.debug("Request to get Dictionary : {}", id);
        Dictionary dictionary = dictionaryRepository.findOne(id);
        DictionaryDTO dictionaryDTO = dictionaryMapper.toDto(dictionary);
        return dictionaryDTO;
    }

    /**
     *  Delete the  dictionary by id.
     *
     *  @param id the id of the entity
     */
    public void delete(String id) {
        log.debug("Request to delete Dictionary : {}", id);
        dictionaryRepository.delete(id);
    }

    /**
     * 获取所有词典信息 分页查询
     * @param hsql
     * @param pageable
     * @return BaseDTO
     */
    public BaseDTO findPageList(Pageable pageable, String hsql, Map<String, Object> mapParas) {
    	log.debug("Request to get all Dictionaries");
    	Page<Dictionary> result = dictionaryRepository.findPageList(hsql, mapParas, pageable);
    	List<DictionaryDTO> dictionaryDTOs = result.getContent().stream().map(
    			dictionaryMapper::toDto).collect(Collectors.toList());
		return BaseDTO.createBaseDTO(result).setFieldList(dictionaryDTOs);
    }

    /**
     * 根据字典类型获取所有字典标签列表
     * @param type
     * @return 标签列表
     */

    public  List<DictionaryDTO> getDictListByType(String type) {
    	log.debug("Request to get all Dictionaries by type ");
    	return dictionaryMapper.toDto(dictionaryRepository.getDictListByType(type));
    }

	/**
	 * 根据字典类型以及字标签名称得到键值
	 * @param key 字典标签名称
	 * @param type 字典类型
	 * @param defaultValue  默认value值
	 * @return  键值
	 */
	public  String getDictValue(String key,String type,String defaultValue) {
		String value=defaultValue;
		DictionaryDTO dictionaryDTO = null;
		if(key!=null && !"".equals(key) && type!=null && !"".equals(type)) {
			dictionaryDTO = getDictListByType(type).stream().filter(m->key.equals(m.getItemKey())).findFirst().orElse(null);
		}
		if(dictionaryDTO!=null) {
			value = dictionaryDTO.getItemValue();
		}
		return value;
	}

	/**
	 *
	 * @param value         键值
	 * @param type          字典类型
	 * @param defaultKey	默认值
	 * @return  key
	 */
	public  String getDictKey(String value,String type,String defaultKey){
		String key=defaultKey;
		DictionaryDTO dictionaryDTO = null;
		if(value!=null && !"".equals(value) && type!=null && !"".equals(type)) {
			dictionaryDTO = getDictListByType(type).stream().filter(m->value.equals(m.getItemValue())).findFirst().orElse(null);
		}
		if(dictionaryDTO!=null) {
			key = dictionaryDTO.getItemKey();
		}
		return key;
	}


	public Dictionary findFirstByDictionaryName(String dictionaryName){
	    return  dictionaryRepository.findFirstByDictionaryName(dictionaryName);
    }
}
